{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "90c80c86",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import json\n",
    "import pandas as pd\n",
    "\n",
    "# 读取 JSON 文件\n",
    "def load_json(file_path):\n",
    "    with open(file_path, \"r\", encoding=\"utf-8\") as f:\n",
    "        return json.load(f)\n",
    "\n",
    "# 加载训练集、验证集、测试集的 JSON 文件\n",
    "train_data = load_json(\"train.json\")\n",
    "val_data = load_json(\"val.json\")\n",
    "test_data = load_json(\"test.json\")\n",
    "\n",
    "# 处理数据为 (userID, itemID, x_label) 格式\n",
    "def convert_to_pairs(data, label):\n",
    "    return [(int(user), int(item), label) for user, items in data.items() for item in items]\n",
    "\n",
    "# 转换数据\n",
    "train_pairs = convert_to_pairs(train_data, 0)\n",
    "val_pairs = convert_to_pairs(val_data, 1)\n",
    "test_pairs = convert_to_pairs(test_data, 2)\n",
    "\n",
    "# 合并数据\n",
    "all_data = train_pairs + val_pairs + test_pairs\n",
    "\n",
    "# 转换为 DataFrame\n",
    "df = pd.DataFrame(all_data, columns=[\"userID\", \"itemID\", \"x_label\"])\n",
    "\n",
    "# 显示 DataFrame\n",
    "# import ace_tools as tools\n",
    "# tools.display_dataframe_to_user(name=\"User-Item Interaction Data\", dataframe=df)\n",
    "\n",
    "# 保存到 CSV 文件\n",
    "df.to_csv(\"tiktok.csv\", index=False, sep=\"\\t\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d42118a8",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}